package net.sppan.base.dao;

import net.sppan.base.entity.Employee;
import net.sppan.base.entity.Equipment;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface IEquipmentDao {
    @Select("SELECT\n" +
            "\te.id,\n" +
            "\te.equipment_code as equipmentCode,\n" +
            "\te.equipment_name as equipmentName,\n" +
            "\te.operate_name as operateName,\n" +
            "\te. equipment_status as  equipmentStatus,\n" +
            "\te.device_id as deviceId,p.employee_name as employeeName " +
            "FROM\n" +
            "\ttb_equipment e left join tb_employee p on e.employee_id = p.id ")
    List<Equipment> getEquipmentList(String equipmentCode);

    @Select("SELECT\n" +
            "            e.id,\n" +
            "            e.equipment_code as equipmentCode,\n" +
            "            e.equipment_name as equipmentName,\n" +
            "            e.operate_name as operateName,\n" +
            "            e. equipment_status as  equipmentStatus,\n" +
            "            e.device_id as deviceId, " +
            "            p.employee_name as employeeName " +
            "            FROM\n" +
            "            tb_equipment e\n" +
            " left join tb_employee p on e.employee_id = p.id\n" +
            " where e.equipment_status = 2")
    List<Equipment> getRentEquipmentList(String equipmentCode);

    @Select("SELECT\n" +
            " (min(p.purchase_num) -\tcount(1) ) lessNum,\n" +
            " p.device_id as deviceId,\n" +
            "min(p.device_name) as deviceName\n" +
            "FROM\n" +
            "\ttb_purchase p\n" +
            "\tLEFT JOIN tb_equipment e ON e.device_id = p.device_id \n" +
            "WHERE 1=1\n" +
            " " +
            "\tGROUP BY p.device_id")
    List<Equipment> getLessDevice();

    @Insert("\tINSERT INTO tb_equipment( equipment_code, equipment_name, operate_name, equipment_status, device_id) VALUES (#{equipmentCode}, #{equipmentName}, #{operateName}, 1, #{deviceId})")
    int addEquipment(Equipment equipment);

    @Select("select id,employee_name as employeeName from tb_employee ")
    List<Employee> getEmployeeList();

    @Update("UPDATE tb_equipment SET equipment_status = 2, employee_id = #{employeeId} WHERE id = #{id} ")
    int rent(Equipment equipment);

    @Update("UPDATE tb_equipment SET equipment_status = 1 WHERE id = #{id} ")
    int returnEquipment(Equipment equipment);
}
